package kr.co.casually;

import java.util.ArrayList;

import kr.co.casually.adapter.KeyWordAdapter;
import kr.co.casually.adapter.KeyWordsDbAdapter;
import kr.co.casually.types.KeyWord;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class KeyWordListActivity extends Activity {
	private KeyWordsDbAdapter dbAdapter;
	private ArrayList<KeyWord> arrayList;
	private ListView listView;
	private KeyWordAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.keyword_list_act);

		arrayList = new ArrayList<KeyWord>();
		listView = (ListView) findViewById(R.id.keyword_list_listview);

		adapter = new KeyWordAdapter(this, arrayList);
		listView.setAdapter(adapter);

		loadSQLLiteKeyWordData();
		listView.setOnItemLongClickListener(mItemLongClickListener);
	}

	// 키워드 목록 가져오기
	private void loadSQLLiteKeyWordData() {
		dbAdapter = new KeyWordsDbAdapter(this);
		dbAdapter.open();

		Cursor results = dbAdapter.fetchAllKeyWords();
		while (results.moveToNext()) {
			KeyWord keyWord = new KeyWord();
			int id = results.getInt(0);
			int genreCode = results.getInt(1);
			String genreName = results.getString(2);
			String startdate = results.getString(3);
			String enddate = results.getString(4);
			String title = results.getString(5);

			keyWord.setId(id);
			keyWord.setGenreCode(genreCode);
			keyWord.setGenreName(genreName);
			keyWord.setStartDate(startdate);
			keyWord.setEndDate(enddate);
			keyWord.setTitle(title);
			addKeyWord(keyWord);

		}

		results.close();
		dbAdapter.close();
		adapter.notifyDataSetChanged();

	}

	// 키워드 리스트에 추가하기
	private void addKeyWord(KeyWord keyWord) {
		arrayList.add(keyWord);
		adapter.notifyDataSetChanged();
	}

	OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View v,
				final int pos, long id) {

			final AlertDialog.Builder b = new AlertDialog.Builder(
					KeyWordListActivity.this);
			b.setMessage(getString(R.string.want_delete));
			b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					onLongListItemClick(arrayList.get(pos).getId());
				}
			});
			b.setNegativeButton("No", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {

				}
			});

			b.show();

			return false;
		}

	};

	// 삭제하기.
	protected void onLongListItemClick(int rowId) {

		dbAdapter = new KeyWordsDbAdapter(this);
		dbAdapter.open();
		dbAdapter.deleteKeyWord(rowId);
		dbAdapter.close();
		arrayList.clear();
		loadSQLLiteKeyWordData();

	}

}
